package com.cmc.cloud.cmclink.doc.controller.admin.so;

import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.convert.AttachmentConvert;
import com.cmc.cloud.cmclink.doc.entity.AttachmentDO;
import com.cmc.cloud.cmclink.doc.service.so.AttachmentService;
import com.cmc.cloud.cmclink.doc.vo.sovo.attachmentvo.AttachmentCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.attachmentvo.AttachmentPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.attachmentvo.AttachmentRespVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.attachmentvo.AttachmentUpdateReqVO;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;


/**
 * 订舱附件 前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 订舱附件 ")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/attachment")
public class AttachmentController {

    @Resource
    private AttachmentService attachmentService;

    @Operation(summary = "创建订舱附件表")
    @PostMapping("/create")
    public CommonResult<Void> createAttachment(@Valid @RequestBody AttachmentCreateReqVO createReqVO) {
        attachmentService.createAttachment(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新订舱附件 ")
    @PostMapping("/update")
    public CommonResult<Void> updateAttachment(@Valid @RequestBody AttachmentUpdateReqVO updateReqVO) {
        attachmentService.updateAttachment(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除订舱附件 ")
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> deleteAttachment(@PathVariable("id") Long id) {
        attachmentService.deleteAttachment(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得订舱附件 ")
    @GetMapping("/get/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AttachmentRespVO> getAttachment(@PathVariable("id") Long id) {
        AttachmentDO attachment = attachmentService.getAttachment(id);
        return CommonResult.success(AttachmentConvert.INSTANCE.convert(attachment));
    }

    @Operation(summary = "获得订舱附件 列表")
    @GetMapping("/list")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<AttachmentRespVO>> getAttachmentList(@RequestParam("ids") Collection<Long> ids) {
        List<AttachmentDO> list = attachmentService.getAttachmentList(ids);
        return CommonResult.success(AttachmentConvert.INSTANCE.convertList(list));
    }

    @Operation(summary = "获得附件 列表")
    @GetMapping("/listAll")
    @Parameter(name = "applyId", description = "附件主信息ID")
    @Parameter(name = "applyType", description = "特种货类型", required = true)
    public CommonResult<List<AttachmentRespVO>> getAttachmentListAll(@RequestParam(value = "applyId", required = false) Long applyId, @RequestParam("applyType") String applyType) {
        return CommonResult.success(attachmentService.getAttachmentListAll(applyId,applyType));
    }

    @Operation(summary = "获得订舱附件 分页")
    @GetMapping("/page")
    public CommonResult<PageResult<AttachmentRespVO>> getAttachmentPage(@Valid AttachmentPageReqVO pageVO) {
        PageResult<AttachmentDO> pageResult = attachmentService.getAttachmentPage(pageVO);
        List<AttachmentRespVO> results = AttachmentConvert.INSTANCE.convertList(pageResult.getList());
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }

    @Operation(summary = "上传或更新附件")
    @PostMapping("/createOrUpdate")
    public CommonResult<Void> createOrUpdateAttachment(@Valid @RequestBody AttachmentUpdateReqVO reqVO) {
        attachmentService.createOrUpdateAttachment(reqVO);
        return CommonResult.success();
    }
}
